var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();

// var queryResponse = require('./queryResponse');
var res_nkl = require('../response/res_nkl');
var api_nkl = require('./api_nkl');

var res_lxd = require('../response/res_lxd');
var api_lxd = require('./api_lxd');

var res_yxw = require('../response/res_yxw');
var api_yxw = require('./api_yxw');

var res_sj = require('../response/res_sj');
var api_sj = require('./api_sj');

var res_wy = require('../response/res_wy');
var api_wy = require('./api_wy');

var res_comm = require('../response/res_comm');
var api_comm = require('./api_comm');

var res_cj = require('../response/res_cj');
var api_cj = require('./api_cj');
//hy
var res_hy = require('../response/res_hy');
var api_hy = require('./api_hy');

var res_hx = require('../response/res_hx');
var api_hx = require('./api_hx');

var res_chq = require('../response/res_chq');
var api_chq = require('./api_chq');

var res_tl = require('../response/res_tl');
var api_tl = require('./api_tl');

var res_wuhui = require('../response/res_wuhui');
var api_wuhui = require('./api_wuhui');

var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.set('port', process.env.PORT || 19000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());

http.listen(app.get('port'), function () {
  console.log('API服务器已启动，开始监听端口' + app.get('port'));
});

//************杨小伟******************
//模拟分页方法
function ypaging(reqBody, resData) {
  var page = reqBody.page;
  var data = {
    totalCount: resData.length,
    lineData: []
  };
  for (var currRow = (page.currentPage - 1) * page.pageSize; currRow < resData.length && currRow < page.currentPage * page.pageSize; currRow++) {
    data.lineData.push(resData[currRow]);
  }
  var responseValue = {
    "data": data,
    "state": '10000'
    // "totalCount": resData.length
  };
  return responseValue;
}

//模拟分页方法
function paging(reqBody, resData) {
  var page = {
    currentPage: Number(reqBody.page.currentPage),
    pageSize: Number(reqBody.page.pageSize)
  };
  var data = [];
  for (var currRow = (page.currentPage - 1) * page.pageSize; currRow < resData.length && currRow < page.currentPage * page.pageSize; currRow++) {
    data.push(resData[currRow]);
  }
  var responseValue = {
    "data": data,
    "state": '10000',
    "totalCount": resData.length
  };
  return responseValue;
}

api_nkl.request(app, res_nkl);
api_yxw.request(app, res_yxw, ypaging,paging,io);
api_lxd.request(app, res_lxd, paging);
api_sj.request(app, res_sj);
api_wy.request(app, res_wy);
api_cj.request(app, res_cj, io);
api_comm.request(app, res_comm);
api_hx.request(app, res_hx);
api_chq.request(app, res_chq);
api_tl.request(app,res_tl);
api_hy.request(app,res_hy);
api_wuhui.request(app,res_wuhui);
